package com.complaint.rest.web;

import com.complaint.rest.proxy.Proxy;
import com.complaint.rest.security.dto.ExampleUserDetails;
import com.complaint.rest.service.ComplaintService;
import com.complaint.rest.service.UserService;
import com.complaint.rest.user.dto.ComplaintForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * Created by Alex
 */
@Controller
@RequestMapping("/complaint")
@SessionAttributes("complaint")
public class ComplaintController {

    @Autowired
    private ComplaintService complaintService;

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String setUpForm(@RequestParam(value = "id", required = false) String id, Map<String, Object> map) {
        if (id == null) {
            map.put("complaint", new ComplaintForm());
        } else {
            ComplaintForm complaint = Proxy.createFromComplaint(complaintService.getComplaint(id));
            map.put("complaint", complaint);
            map.put("author", userService.getUser(complaint.getAuthorId()));
        }
        return "complaint";
    }
//    public Complaint setUpForm(@RequestParam(value="id", required = false) String id) {
//        if (id == null) {
//            return new Complaint();
//        } else {
//            return complaintService.getComplaint(id);
//        }
//    }

    @RequestMapping(params = "create", method = RequestMethod.POST)
    public String createComplaint(@ModelAttribute("complaint") ComplaintForm complaint, BindingResult result) {
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        ExampleUserDetails user = (ExampleUserDetails) authentication.getPrincipal();
        complaint.setAuthorId(user.getId());
        complaint.setPublishDate(new Date());
        complaintService.addComplaint(Proxy.createFromComplaintForm(complaint));
        return "redirect:/index";
    }

    @RequestMapping(params = "update", method = RequestMethod.POST)
    public String updateComplaint(@ModelAttribute("complaint") ComplaintForm complaint, BindingResult result) {
        complaintService.updateComplaint(Proxy.createFromComplaintForm(complaint));
        return "redirect:/index";
    }

    @RequestMapping(params = "delete", method = RequestMethod.POST)
    public String deleteComplaint(@ModelAttribute("complaint") ComplaintForm complaint, BindingResult result) {
        complaintService.removeComplaint(Proxy.createFromComplaintForm(complaint));
        return "redirect:/index";
    }
}
